package org.iflytek;

import org.apache.shiro.SecurityUtils;
import  org.apache.shiro.authc.AuthenticationException;
import  org.apache.shiro.authc.UsernamePasswordToken;
import  org.apache.shiro.config.IniSecurityManagerFactory;
import  org.apache.shiro.mgt.SecurityManager;
import  org.apache.shiro.subject.Subject;
import  org.apache.shiro.util.Factory;



public class ShiroDemo {
    public static void main(String[] args) {
     //1 读取配置文件 初始化工厂对象
        Factory<SecurityManager> factory=new IniSecurityManagerFactory("src/main/resources/shiro.ini");
        //2 获取SecurityManager实例
        SecurityManager securityManager=factory.getInstance();//单例模式 懒加载
        //3 将SecurityManager绑定到工具类
        SecurityUtils.setSecurityManager(securityManager);
        //4 通过SecurityUtils得到当前登录的用户
        Subject subject=SecurityUtils.getSubject();
        //5 创建令牌对象
        UsernamePasswordToken token=new UsernamePasswordToken("lisi","123");
        try {
            //6 登录
            subject.login(token);
            System.out.println("身份验证成功!");
        }catch (AuthenticationException e) {
                   e.printStackTrace();
            System.out.println("身份验证失败!");

        }
        //7 注销
        subject.logout();

    }
}
